﻿using DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication.Core;
using WebApplication.Models;

namespace WebApplication.Controllers
{
    public class ReportsController : Controller
    {
        DataManager dm;
        public ReportsController()
        {
            dm = new DataManager();
        }

        public ActionResult List()
        {
            return View(dm.ReportsManager.GetAll());
        }

        public ActionResult ViewReport(int id)
        {
            var report = dm.ReportsManager.GetById(id);
            if (report == null)
                return HttpNotFound();

            return View(new ReportSettingsModel(report));
        }

        public ActionResult GetReportsControls(FormCollection form, int idReport)
        {
            var report = dm.ReportsManager.GetById(idReport);
            if (report == null)
                return HttpNotFound();
            var reportSettings = new ReportSettingsModel(report);
            Dictionary<string, object> reportParameters = new Dictionary<string, object>();
            foreach (var item in reportSettings.ReportParameters)
            {
                reportParameters.Add(item.Name, GetParameterValue(form[item.Name], item.DataType));
            }
            Session["reportParameters"] = reportParameters;
            Session["reportsSettings"] = reportSettings;
            return Redirect("~/Reports.aspx");
        }

        private dynamic GetParameterValue(string value, EnumDataTypes type)
        {
            switch (type)
            {
                case EnumDataTypes.Date:
                    return DateTime.Parse(value);
                case EnumDataTypes.DeployObject:
                    return Int32.Parse(value);
                case EnumDataTypes.Numeric:
                    return Double.Parse(value);
                case EnumDataTypes.Server:
                    return Int32.Parse(value);
                case EnumDataTypes.Symbol:
                    return value;
                default:
                    throw new ArgumentException("Неверное значение type");
            }
        }

    }
}
